<?php
namespace App\Components;
/**
 * Created by PhpStorm.
 * User: xiandanjia
 * Date: 2018/12/25
 * Time: 下午 5:39
 */

class YunClear{
    const ImgAppID = 201812251343;
    const TextAppID = 201812255659;
    const AppKey = '23BDE4A6496EFC7C35F24D77CC7FADE8';
    public static function Img($app,$type,$url,$uid){//201812251343
        $time = date('Y-m-d H:i:s');
        $data['app_id'] = self::ImgAppID;
        $data['version'] = '2.0';
        $data['timestamp'] = $time;
        $data['sign'] = self::get_signature(self::ImgAppID.','.$time,self::AppKey);

        $biz['id'] =time().$uid;
        $biz['relation_id'] = $app.'-'.$type.'-'.$uid;
        $biz['user_id'] = $uid;
        $biz['publish_date'] = $time;

        $content['type'] = 'image';
        $content['content'] = $url;
        $content['master '] = 1;

        $biz['contents'] = [$content];
        $data['json_data'] = json_encode($biz);
        $res = \App\Components\Tool::curl('http://api.yunjingwang.cn:8080/audit',$data);
        return self::getResult($res);
    }
    public static function Slogan($app,$type,$slogan,$uid){
        $time = date('Y-m-d H:i:s');
        $data['app_id'] = self::TextAppID;
        $data['version'] = '2.0';
        $data['timestamp'] = $time;
        $data['sign'] = self::get_signature(self::TextAppID.','.$time,self::AppKey);

            $biz['id'] =time().$uid;
            $biz['relation_id'] = $app.'-'.$type.'-'.$uid;
            $biz['user_id'] = $uid;
            $biz['publish_date'] = $time;

                $content['type'] = 'text';
                $content['content'] = $slogan;
                $content['master '] = 1;

            $biz['contents'] = [$content];
        $data['json_data'] = json_encode($biz);
        $res = \App\Components\Tool::curl('http://api.yunjingwang.cn:8080/audit',$data);
        return self::getResult($res);
    }
    public static function getResult($json){
        $json = json_decode($json,true);
        Tool::log($json,'yunjing.log');
        if (!json_last_error()){
            if ($json['code']==1001){
                return 1;
            }else if ($json['code']==1002){
                return 0;
            }else{
                return 2;
            }
        }
        return 2;
    }
    public static function get_signature($str, $key)
    {
        $signature = "";
        if (function_exists('hash_hmac'))
        {
            $signature = base64_encode(hash_hmac("sha1", $str, $key, true));
        }
        else
        {
            $blocksize    = 64;
            $hashfunc    = 'sha1';
            if (strlen($key) > $blocksize)
            {
                $key = pack('H*', $hashfunc($key));
            }
            $key    = str_pad($key,$blocksize,chr(0x00));
            $ipad    = str_repeat(chr(0x36),$blocksize);
            $opad    = str_repeat(chr(0x5c),$blocksize);
            $hmac     = pack(
                'H*',$hashfunc(
                    ($key^$opad).pack(
                        'H*',$hashfunc(
                            ($key^$ipad).$str
                        )
                    )
                )
            );
            $signature = base64_encode($hmac);
        }

        return $signature;
    }
}